<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }" class="el-item"><i class="el-icon-s-promotion"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加类别</el-breadcrumb-item>
    </el-breadcrumb>
    <div style="margin: 20px;"></div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="类别名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="父级ID" prop="ParentId">
        <el-input v-model="ruleForm.ParentId"></el-input>
      </el-form-item>
      <el-form-item label="类别图标" prop="icon">
        <el-input v-model="ruleForm.icon"></el-input>
      </el-form-item>
      <el-form-item label="序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item label="关键词" prop="keywords">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch
            v-model="ruleForm.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#999">
        </el-switch>
      </el-form-item>
      <el-form-item label="显示导航栏">
        <el-switch
            v-model="ruleForm.isDisplay"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#999">
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '测试数据1',
        keywords: '测试简介1',
        sort:'1',
        isDisplay:1,
        icon:"https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png",
        ParentId: '1',
        enable: 1
      },
      rules: {
        ParentId: [
          { required: true, message: '请输入父级ID', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入名字', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        keywords: [
          { required: true, message: '请输入关键词', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        sort:[
          { required: true, message: '请输入序号', trigger: 'blur' },
          { pattern:/^\d{1}|[1-9]{1}[0-9]?$/, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        icon: [
          { required: true, message: '请输入图标地址', trigger: 'blur' },
          { min: 4, max: 100, message: '长度在 4 到 100 个字符', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:9080/categories/add-new";
          let formData = this.qs.stringify(this.ruleForm);
          this.axios.post(url,formData).then((response)=>{
            let responsebody = response.data;
            console.log(responsebody);
            if (responsebody.state==20000){
              this.$message({
                showClose:true,
                message:'添加类别成功',
                type: 'success'
                  });
              this.resetForm(formName);
            }else if (responsebody.state==40400){
              this.$message({
                showClose:true,
                message:responsebody.message,
                type: 'error'
              });
            }else if (responsebody.state==40900){
              this.$message({
                showClose:true,
                message:responsebody.message,
                type: 'error'
              });
            }
          })
          console.log(url,formData);
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.el-item{
  font-size: 20px;
}
</style>